package com.jinmei.web.action;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jinmei.model.Employee;
import com.jinmei.model.WorkSchedule;
import com.jinmei.service.EmployeeService;
import com.jinmei.service.WorkScheduleService;
import com.jinmei.utils.NumberUtil;
import com.jinmei.utils.StringUtil;
import com.jinmei.web.WorkScheduleDayDTO;
import com.jinmei.web.ws.WorkScheduleEntityParser;

public class WorkScheduleDayAction extends WorkScheduleDayBaseAction {

	private static final long serialVersionUID = 1201939537081442025L;
	private static Logger logger = Logger.getLogger(WorkScheduleDayAction.class);
	
	private WorkScheduleService workScheduleService;
	private EmployeeService employeeService;
	
	private String workDate;
	private String employeeId;
	private String employeeName;
	
	private List<WorkScheduleDayDTO> resultList = new ArrayList<WorkScheduleDayDTO>();
	
	public String getAllWorkScheduleDay() throws Exception {
		setUserSessionIdAndUsername();
		
		List<Employee> allEmployees = getEmployeeService().findAllEmployees();
		List<WorkSchedule> workSchedulesByDate = getWorkScheduleService().
				getWorkSchedulesByDate(StringUtil.convertString2Date(getWorkDate()));
		constructEmpolyees2Dtos(allEmployees, workSchedulesByDate);
		
		logger.info("resultList size: " + resultList.size());
		return SUCCESS;
	}
	
	public String getWorkScheduleDayByEmployee() throws Exception {
		setUserSessionIdAndUsername();
		
		List<Employee> employees = getEmployeeService().findEmployeeByfNumberAndName(
				getEmployeeId(), getEmployeeName());
		List<WorkSchedule> workSchedulesByDate = getWorkScheduleService().
				getWorkSchedulesByDate(StringUtil.convertString2Date(getWorkDate()));
		constructEmpolyees2Dtos(employees, workSchedulesByDate);
		
		return SUCCESS;
	}
	
	public String updateAllWorkScheduleDay() throws Exception {
		setUserSessionIdAndUsername();
		
		List<WorkSchedule> oldWSByDate = getWorkScheduleService().getWorkSchedulesByDate(
				StringUtil.convertString2Date(getWorkDate()));
		WorkScheduleEntityParser parser = new WorkScheduleEntityParser(getResultList(), 
				oldWSByDate, StringUtil.convertString2Date(getWorkDate()));
		
		getWorkScheduleService().updateWorkSchedules(oldWSByDate, parser.getNewWSByDate());
		
		getWorkScheduleService().sendNotification(StringUtil.convertString2Date(getWorkDate()), 
				parser.getOldMultiWSMapByEmployeeId(), parser.getNewMultiWSMapByEmployeeId());
		
//		addActionMessage(JinmeiConsts.MSG_SAVE_SUCCESS);
		return SUCCESS;
	}
	
	private void constructEmpolyees2Dtos(List<Employee> employees, List<WorkSchedule> workSchedulesByDate) {
		if (employees != null) {
			resultList.clear();
			for (Employee employee : employees) {
				WorkScheduleDayDTO dto = new WorkScheduleDayDTO();
				dto.setFid(employee.getfId());
				dto.setFnumber(employee.getfNumber());
				dto.setFname(employee.getfName());
				dto.setFsex(employee.getfSex());
				dto.setWorkDate(StringUtil.convertString2Date(getWorkDate()));
				
				List<WorkSchedule> workingTypes = findWorkScheduleByEmployeeId(workSchedulesByDate, employee.getfId());
				if (workingTypes != null) {
					for (WorkSchedule ws : workingTypes) {
						// 0:calss A  	1:class B 	2: calss C 3: not on duty
						switch (ws.getWorkingType().intValue()) {
						case 0:
							dto.setIsWorkingTypeA(true);
							break;
						case 1:
							dto.setIsWorkingTypeB(true);
							break;
						case 2:
							dto.setIsWorkingTypeC(true);
							break;
						case 3:
							dto.setIsNotOnDuty(true);
						default:
							break;
						}
					}
				}
				resultList.add(dto);
			}
		}
	} 
	
	private List<WorkSchedule> findWorkScheduleByEmployeeId(List<WorkSchedule> list, Integer empolyeeId) {
		if (list == null) return new ArrayList<WorkSchedule>();
		
		List<WorkSchedule> results = new ArrayList<WorkSchedule>();
		for (WorkSchedule workSchedule : list) {
			if (NumberUtil.equels(workSchedule.getEmployee().getfId(), empolyeeId)) {
				results.add(workSchedule);
			}
		}
		return results;
	}

	public String getWorkDate() {
		return workDate;
	}

	public void setWorkDate(String workDate) {
		this.workDate = workDate;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public WorkScheduleService getWorkScheduleService() {
		return workScheduleService;
	}

	public void setWorkScheduleService(WorkScheduleService workScheduleService) {
		this.workScheduleService = workScheduleService;
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public List<WorkScheduleDayDTO> getResultList() {
		return resultList;
	}

	public void setResultList(List<WorkScheduleDayDTO> resultList) {
		this.resultList = resultList;
	}

}
